//
//  NewFeatureViewController.swift
//  WeiBoSwift
//
//  Created by rong on 15/6/27.
//  Copyright © 2015年 rong. All rights reserved.
//

import UIKit

private let reuseIdentifier = "Cell"

class NewFeatureViewController: UICollectionViewController {

    //界面布局
    let layout = UICollectionViewFlowLayout()
    
    init() {
        super.init(collectionViewLayout: layout)
    }
    
    required init(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //layout 的布局在这里设置
    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)
        
        //设置布局
        layout.itemSize = view.bounds.size
        layout.minimumInteritemSpacing = 0
        layout.minimumLineSpacing = 0
        
        layout.scrollDirection = UICollectionViewScrollDirection.Horizontal
        
        collectionView?.pagingEnabled = true
        collectionView?.showsHorizontalScrollIndicator = false
        //collectionView?.bounces = false
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()

        // Register cell classes
        // !!!要改这里
        self.collectionView!.registerClass(NewFeatureViewCell.self, forCellWithReuseIdentifier: reuseIdentifier)
    }

    // UICollectionViewFlowLayout 的布局要设置在这里
    
    
    //override func collectionView(collectionView: uicollectionview)
    
    //图片数量
    let imgCnt = 4
    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return imgCnt
    }
    
    
    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
    
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! NewFeatureViewCell
        
        cell.imgIndex = indexPath.item
       // print("cell.imgIndex == > \(indexPath.item)")
        
        
        return cell
    }
    
    
    //注意： cell ＝＝》 是上一个显示的cell
    //      indexPath ＝＝》 是上一个显示的 indexPath
    //停止滚动 调用的函数
    override func collectionView(collectionView: UICollectionView, didEndDisplayingCell cell: UICollectionViewCell, forItemAtIndexPath indexPath: NSIndexPath) {
        
        //获取当前显示的  cell的indexPath
        let path = collectionView.indexPathsForVisibleItems().last!
        
       // print("didEndDisplayingCell == > \(indexPath.item) ==> \(imgCnt - 1)")
        
        if path.item == imgCnt - 1{
            
            (collectionView.cellForItemAtIndexPath(path) as! NewFeatureViewCell).showStartBtn()
        
        }
    }
}

